<?php
namespace Application\Api\Controller;

class Invite extends Base
{

    private function getService()
    {
        return $this->get(SERVICE_INVITE);
    }

    public function indexAction()
    {
        $data = $this->getRequestData();
        $send = $this->getService()->getIndex($data['page'], $data['limit']);
        $this->sendSuccess($send);
    }

    public function newAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $invite['dry_invitor'] = $userId;
        $invite['dry_acceptor'] = $data['dry_acceptor'];
        $invite['dry_team'] = $data['dry_team'];
        $temp = $service->getOneByFields($invite);
        $id = 0;
        if($temp == '' || $temp->dry_invite_status == 2){
            if($data['id']){
                unset($data['id']);
            }
            unset($data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
            $data = addDateTime($data, THE_TIME);
            $data['dry_invitor'] = $userId;
            $data['dry_sort'] = isset($data['dry_sort']) ? $data['dry_sort'] : 0;
            $data['dry_invite_status'] = 0;
            $id = $service->add($data);
        }
        $send = [
            'id' => $id
        ];
        $this->sendSuccess($send);
    }

    public function editAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $service = $this->getService();
        $id = $data['id'];
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
        $data = addDateTime($data, THE_TIME);
        $rows = $service->update($data, $id);
        $send = [
            'rows' => $rows
        ];
        $this->sendSuccess($send);
    }

    public function getListByFieldsOrWhereAction()
    {
        $data = $this->getRequestData();
        $device = DEVICE_WEIXIN_SP;
        if(setAndNotEmpty($data, 'device')){
            $device = $data['device'];
        }
        $serviceUser = $this->get(SERVICE_USER);
        $userId = $serviceUser->checkToken($data, $device);
        if(!$userId){
            $this->sendFail('TOKEN_ERROR', '', __LINE__);
            return false;
        }
        $data['dry_invitor'] = $userId;
        $data['dry_acceptor'] = $userId;
        unset($data['id'],$data['device'],$data['token'],$data['timestamp'],$data['rand'],$data['sign']);
        $list = $this->getService()->getMoreByFieldsOrWhere($data);
        $data = [
            'list' => $list
        ];
        $this->sendSuccess($data);
    }
}